
/**
 * @author XIAO-QIAO.LIU
 */
package ipebg.mzk.provider.system.dao.impl;

import ipebg.mzk.common.core.dao.impl.DBDao;
import ipebg.mzk.common.util.Page;
import ipebg.mzk.facade.entity.MsFile;
import ipebg.mzk.provider.system.dao.IMsFileDao;
import ipebg.mzk.provider.system.dao.QueryForPagesDAO;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

@Repository("msFileDao")
public class MsFileDao extends DBDao implements IMsFileDao {
	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(MsFileDao.class);

	@Resource(name="queryForPagesDAO")
	private QueryForPagesDAO<MsFile> pagesDAO;
	
	@SuppressWarnings("unchecked")
	public MsFileDao() {
		super(MsFile.class);
	}

	@Override
	public void save(MsFile fileObj) throws Exception {
		try {
			super.save(fileObj);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
	}

	@Override
	public void update(MsFile fileObj) throws Exception {
		try {
			super.update(fileObj);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
	}

	@Override
	public void delete(MsFile fileObj) throws Exception {
		try {
			super.delete(fileObj);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
	}

	@Override
	public List<MsFile> getAll() throws Exception {
		List<MsFile> files = null;
		try {
			files = findAll(null);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
		return files;
	}

	@Override
	public Page getFiles(String realFileName, int from, int length) throws Exception {
		Page filePage = null;
		String hql = "from MsFile u where 1=1";
		if (realFileName != null && realFileName.trim().length() > 0) {
			hql += " and u.fileRealName like '%" + realFileName + "%'";
		}
		hql += " order by u.uploadDatetime desc ";
		log.info("query For File List:" + hql);
		try {
			filePage =pagesDAO.findPageByhqlQuery( from, length, hql);// pageServiceImpl.findPageByhqlQuery(from, length, hql);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
		return filePage;
	}

	
	
	@Override
	public void batchSaveFiles(List<MsFile> files) throws Exception {
		try {
			super.batchSave(files);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
	}

	@Override
	public void batchDeleteFiles(List<MsFile> files) throws Exception {
		try {
			super.batchDelete(files);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
	}

	@Override
	public MsFile findFile(String id) throws Exception {
		MsFile file = null;
		try {
			file = (MsFile) super.findById(id);
		} catch (Exception e) {
			log.error(e);
			throw e;
		}
		return file;
	}
}
